package dataBase;

import javax.jdo.*;

import com.google.appengine.api.datastore.Key;


public class Manager { 
    PersistenceManager pm = null; 
    public Manager() { 
            pm = PMF.get().getPersistenceManager(); 
            System.out.println("pm " + pm);
    } 
    public PersistenceManager getPersistenceManager(){ 
            return pm; 
    } 
    public boolean Save(Object obj) { 
            Transaction tx = null; 
            try { 
            		tx =  pm.currentTransaction();
                    tx.begin(); 
                    pm.makePersistent(obj); 
                    tx.commit(); 
                    System.out.println("save in DB"+ obj); 
                    return true; 
            } 
            catch(Exception ex) { 
                    System.out.println(ex.getMessage()); 
                    System.out.println(ex.getLocalizedMessage()); 
                    if(tx.isActive()) 
                            tx.rollback(); 
                    System.out.println("problam in save"); 
                    return false; 
            } 
    } 
    public boolean Delete(Object obj){
            Transaction tx = null; 
            try { 
                    tx = pm.currentTransaction(); 
                    tx.begin(); 
                    pm.deletePersistent(obj); 
                    tx.commit(); 
                    System.out.println("delete from DB"); 
                    return true; 
            } 
            catch(Exception ex){ 
                    System.out.println(ex.getMessage()); 
                    return false; 
            } 
            finally { 
                    if(tx.isActive()) 
                            tx.rollback(); 
            } 
    } 
    public Object getObjects(Object obj) { 
            Query query = pm.newQuery(obj.getClass()); 
            Object reObj = query.execute(); 
            return reObj; 
    } 
    public Object getObjectById(Object obj, Long uid) { 
            Object reObj = pm.getObjectById(obj.getClass(), uid); 
            return reObj; 
    } 
    public Object getObjectById(Object obj, Key key) { 
        Object reObj = pm.getObjectById(obj.getClass(), key); 
        return reObj; 
}
    public Object executeJDOSQL(String sql){ 
    		pm.newQuery(sql);
            Object reObj = pm.newQuery(sql).execute();
            return reObj; 
    } 
} 
